package com.atguigu.lease.web.admin.service.impl;

import com.atguigu.lease.model.entity.SystemPost;
import com.atguigu.lease.model.entity.SystemUser;
import com.atguigu.lease.web.admin.mapper.SystemPostMapper;
import com.atguigu.lease.web.admin.mapper.SystemUserMapper;
import com.atguigu.lease.web.admin.service.SystemPostService;
import com.atguigu.lease.web.admin.service.SystemUserService;
import com.atguigu.lease.web.admin.vo.system.user.SystemUserItemVo;
import com.atguigu.lease.web.admin.vo.system.user.SystemUserQueryVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
 * @author liubo
 * @description 针对表【system_user(员工信息表)】的数据库操作Service实现
 * @createDate 2023-07-24 15:48:00
 */
@Service
public class SystemUserServiceImpl extends ServiceImpl<SystemUserMapper, SystemUser>
        implements SystemUserService {
    @Autowired
    SystemPostMapper systemPostMapper;
    @Autowired
    SystemUserMapper systemUserMapper;
    @Autowired
    SystemPostService systemPostService;
    @Override
    public IPage<SystemUserItemVo> pageSystemUserByQuery(Page<SystemUser> page, SystemUserQueryVo queryVo) {
        QueryWrapper<SystemUser> queryWrapper = new QueryWrapper<>();
        String name = queryVo.getName();
        String phone = queryVo.getPhone();
        queryWrapper.like(StringUtils.hasText(name), "name", name);
        queryWrapper.like(StringUtils.hasText(phone), "phone", phone);
        IPage<SystemUser> iPage = baseMapper.selectPage(page, queryWrapper);


        List<SystemUser> systemUsers = iPage.getRecords();
        List<SystemPost> systemPosts = systemPostMapper.selectList(null);
        Map<Long, String> postMap = systemPosts.stream().collect(Collectors.toMap(SystemPost::getId, SystemPost::getName));

        List<SystemUserItemVo> systemUserItemVos= new ArrayList<>();
        for (SystemUser systemUser : systemUsers) {
            SystemUserItemVo systemUserItemVo = new SystemUserItemVo();
            BeanUtils.copyProperties(systemUser, systemUserItemVo);
            systemUserItemVo.setPostName(postMap.get(systemUser.getPostId()));
            systemUserItemVos.add(systemUserItemVo);
        }
        IPage<SystemUserItemVo> SystemUserItemVo = new Page<>();
        SystemUserItemVo.setCurrent(iPage.getCurrent());
        SystemUserItemVo.setSize(iPage.getSize());
        SystemUserItemVo.setTotal(iPage.getTotal());
        SystemUserItemVo.setRecords(systemUserItemVos);
        return SystemUserItemVo;
    }

    @Override
    public SystemUserItemVo getSystemUserById(Long id) {
        SystemUser systemUser = systemUserMapper.selectById(id);
        systemUser.setPassword(null);
        SystemPost systemPost = systemPostService.getById(systemUser.getPostId());
        SystemUserItemVo systemUserItemVo = new SystemUserItemVo();
        BeanUtils.copyProperties(systemUser, systemUserItemVo);
        systemUserItemVo.setPostName(systemPost.getName());
        return systemUserItemVo;
    }
}




